package com.syl.starter.itools.common.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 注解工具类
 *
 * @author syl
 * @create 2018-10-15 18:12
 **/
public class AnnotationUtils {

    /**
     * 获取带有某注解的class 字段
     * @param targetClass
     * @param annotationClass
     * @return
     */
    public static List<Field> getValidField(Class targetClass, Class annotationClass){
        Field[] fields = ReflectUtils.getPrivateField(targetClass);
        List<Field> list = new ArrayList<>(fields.length);
        for (Field field : fields) {
            Annotation annotation = field.getAnnotation(annotationClass);
            if(annotation == null)
                continue;
            list.add(field);
        }
        return list;
    }

    /**
     * 获取带有某注解的class Method
     * @param targetClass
     * @param annotationClass
     * @return
     */
    public static List<Method> getValidMethod(Class targetClass, Class annotationClass){
        Method[] methods = targetClass.getDeclaredMethods();
        List<Method> methodList = new ArrayList<>();
        for (Method method : methods) {
            Annotation annotation = method.getAnnotation(annotationClass);
            if(annotation == null)
                continue;
            methodList.add(method);
        }
        return methodList;
    }

}
